package com.imicode.tel.book.server;

import com.imicode.tel.book.Message;
import com.imicode.tel.book.OnlineBookPrx;
import com.imicode.tel.book.OnlineBookPrxHelper;
import com.imicode.tel.message._SMSServiceDisp;

import Ice.Communicator;
import Ice.Current;
import Ice.ObjectAdapter;
import IceBox.Service;

public class SMSServiceImpl extends _SMSServiceDisp implements Service {
	
	private static final long serialVersionUID = 1L;
	private ObjectAdapter _adapter;

	@Override
	public void sendSms(String msg, Current __current) {
		System.out.println("send message: " + msg);
		if(msg.startsWith("book")) {
			//ice box
//			Ice.ObjectPrx base = _adapter.getCommunicator().stringToProxy("OnlineBook");
			//ice registry
//			Ice.ObjectPrx base = _adapter.getCommunicator().stringToProxy("OnlineBook@OnlineBookAdapter");
			//ice grid
			Ice.ObjectPrx base = _adapter.getCommunicator().stringToProxy("OnlineBook");
			OnlineBookPrx onlineBook = OnlineBookPrxHelper.checkedCast(base);
			
			Message bookMsg = new Message();
			bookMsg.name = "《struts2》";
			bookMsg.type = 3;
			bookMsg.price = 19.99;
			bookMsg.content = msg;
			
			onlineBook.bookTick(bookMsg);
		}
	}

	@Override
	public void start(String name, Communicator communicator, String[] args) {
		_adapter = communicator.createObjectAdapter(name);
		Ice.Object object = this;
		_adapter.add(object, communicator.stringToIdentity(name));
		_adapter.activate();
		System.out.println("***********************" + name + " started.");
	}

	@Override
	public void stop() {
		System.out.println("****************************" + this._adapter.getName() + " stoped.");
		_adapter.destroy();
	}

}
